<template>
  <div>
    <div class="container">
      <div class="left">
        <AirTicketsHeader
          ref="clearFilter"
          :data="airList"
          @filter="getFilterList"
          @filterClear="getList"
        />
        <TableHeader />
        <AirTicketsItem v-if="item" :data="item" v-for="item in flights" :key="item.id" />
        <!-- 分页 -->
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
      <!-- 完成右边服务布局 -->
      <div class="right">
        <div class="service">
          <div class="header">
            <div>
              <i style="color: rgb(64, 158, 255);" class="iconfont iconweibiaoti-_huabanfuben"></i>
              <p>航协认证</p>
            </div>
            <div>
              <i style="color: green;" class="iconfont iconbaozheng"></i>
              <p>出行保证</p>
            </div>
            <div>
              <i style="color: rgb(64, 158, 255);" class="iconfont icondianhua"></i>
              <p>7x24</p>
            </div>
          </div>
          <div class="footer">
            <p>免费客服电话：4006345678转2</p>
          </div>
        </div>
        <div class="history">
          <h2>历史查询</h2>
          <ul>
            <li
              @click="changeroute(item)"
              v-for="(item,index) in $store.state.user.history"
              :key="index"
            >
              <div class="h_l">
                <p>{{item.departCity}} - {{item.destCity}}</p>
                <span>{{item.departDate}}</span>
              </div>
              <div class="h_r">选择</div>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import AirTicketsHeader from '@/components/air/AirTicketsHeader'
import TableHeader from '@/components/air/TableHeader'
import AirTicketsItem from '@/components/air/AirTicketsItem'
export default {
  components: {
    AirTicketsHeader,
    TableHeader,
    AirTicketsItem
  },
  data() {
    return {
      airList: {},
      // 显示的数据
      // flights: [],
      // 当前页
      currentPage: 1,
      pageSize: 5,
      total: 0,
      filterList: [],
      // 所有数组
      flightsList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 显示条数发生改变
    handleSizeChange(newSize) {
      this.pageSize = newSize
    },
    // 页数发生改变
    handleCurrentChange(newPage) {
      this.currentPage = newPage
    },
    async getList() {
      const { data: res } = await this.$axios.get('/airs', {
        params: this.$route.query
      })
      this.airList = res
      this.total = res.total

      this.flightsList = res.flights.map(x => {
        return {
          ...x,
          flag: false
        }
      })
    },
    // 过滤监听事件
    getFilterList(arr) {
      this.flightsList = arr.map(x => {
        return {
          ...x,
          flag: false
        }
      })
      this.total = arr.length
    },
    // 历史查询点击
    changeroute(item) {
      this.$router.push({ path: '/air/flights', query: item })
    }
  },
  computed: {
    flights() {
      return this.flightsList.slice(
        (this.currentPage - 1) * this.pageSize,
        this.pageSize * this.currentPage
      )
    }
  },
  watch: {
    $route() {
      this.$refs.clearFilter.clear()
      this.currentPage = 1
    }
  }
}
</script>
<style lang="less" scoped>
.container {
  padding-top: 20px;
  width: 1000px;
  min-width: 1000px;
  margin: 0 auto;
  // height:600px;
  // background:pink;
  display: flex;
}
.left {
  flex: 1;
  // background:orange;
}
.right {
  margin-left: 15px;
  width: 238px;
  // background: red;
  .service {
    border: 1px solid #dddddd;
    margin-bottom: 10px;
    .header {
      align-items: center;
      height: 84px;
      display: flex;
      justify-content: space-around;
      div {
        flex: 1;
        text-align: center;
        i {
          font-size: 40px;
        }
        p {
          font-size: 14px;
        }
      }
    }
    .footer {
      height: 32px;
      background: #f6f6f6;
      font-size: 14px;
      line-height: 32px;
      padding-left: 10px;
    }
  }
  .history {
    padding: 0 10px;
    border: 1px solid #ddd;
    max-height: 465px;
    overflow: hidden;
    h2 {
      padding: 10px 0;
      font-size: 18px;
      font-weight: 300;
      border-bottom: 1px solid #ddd;
    }
    ul {
      list-style: none;
      padding: 0;
      li {
        cursor: pointer;
        padding: 10px 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .h_l {
          p {
            font-size: 14px;
          }
          span {
            font-size: 12px;
            color: #666666;
          }
        }
        .h_r {
          padding: 2px 10px;
          background: #ffa500;
          font-size: 12px;
          color: #fff;
          border-radius: 4px;
        }
      }
    }
  }
}
.el-pagination {
  margin: 20px 0;
}
</style>